package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {

        Setmeal setmeal=new Setmeal();

        BeanUtils.copyProperties(setmealDTO,setmeal);

        //新增套餐
        setmealMapper.addSetmeal(setmeal);
        Long setmealId=setmeal.getId();

        //添加菜品套餐关系
        List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();
        setmealDishMapper.addSetmealDish(setmealDishes,setmealId);

    }

    /**
     * 分页查询套餐信息
     * @param pageQueryDTO
     * @return
     */
    @Override
    public PageResult findSetmeal(SetmealPageQueryDTO pageQueryDTO) {

        PageHelper.startPage(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());

        Page<SetmealVO> p=setmealMapper.findSetmeal(pageQueryDTO);

        PageResult pageResult=new PageResult(p.getTotal(),p.getResult());

        return pageResult;
    }

    /**
     * 根据id查询套餐信息
     * @param id
     * @return
     */
    @Override
    public SetmealVO findById(Long id) {

        SetmealVO setmealVO=setmealMapper.findById(id);

        List<SetmealDish> sds=setmealDishMapper.findById(id);

        setmealVO.setSetmealDishes(sds);

        return setmealVO;
    }

    /**
     * 修改套餐信息
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void change(SetmealDTO setmealDTO) {

        //1 修改套餐信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.change(setmeal);
        //2 修改套餐菜品关系(先删再加)
        List<SetmealDish> list=setmealDTO.getSetmealDishes();

        setmealDishMapper.delete(setmealDTO.getId());
        setmealDishMapper.addSetmealDish(list,setmealDTO.getId());

    }

    /**
     * 改变套餐起售状态
     * @param status
     */
    @Override
    public void changeStatus(Integer status,Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.change(setmeal);
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void drop(List<Long> ids) {

        for (Long id : ids) {

            Integer status=setmealMapper.findStatus(id);
            if(status == StatusConstant.ENABLE){
                BaseException ex=new BaseException("套餐正在起售中");
                throw ex;
            }else if(status == StatusConstant.DISABLE){
                setmealMapper.drop(id);
                setmealDishMapper.delete(id);
            }

        }

    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
